package com.spring.oaassist.web.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.spera.aliyunContent.AliyunContent;
import com.spera.aliyunUtil.SperaOSSUtil;
import com.spera.shopoa.util.ShopOaSessionUtil;
import com.spera.shopoa.util.tools;
import com.spring.oaassist.web.model.Teacher;
import com.spring.oaassist.web.service.TeacherService;
import com.spring.security.core.annotation.OaUserPermission;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.core.util.UDate;
import com.spring.security.filestream.util.TokenUtil;

/**
 * 教师信息维护
 * @author wojunyu
 *
 */
@Controller
@RequestMapping(value = "/teacher")
public class TeacherController {
	@Resource private TeacherService teacherService;
	
	/**
	 * 主页
	 * @return
	 */
	@OaUserPermission(value="/teacher/index")
	@RequestMapping(value = "/index")
	public ModelAndView index(){
		return new ModelAndView("main/teacher/index");
	}
	
	
	/**
	 * 查询列表
	 * @param kids_park_id
	 * @param page
	 * @param pageSize
	 * @param request
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/queryTearcherList")
    @ResponseBody
    public Object queryTearcherList(String kids_park_id,String page,String pageSize,HttpServletRequest request){
        Map<String, Object> parm = new HashMap<String, Object>();
        Map<String, Object> retD = new HashMap<String, Object>();
//    	parm.put("kids_park_id",kids_park_id);
    	PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(pageSize));
        List<Map<String,Object>> list = teacherService.queryTearcherList(parm);
        List<Map<String,Object>> nlist = Lists.newArrayList();
        PageInfo pageObj = new PageInfo(list);
        for(Map<String,Object> m:list){
        	String user_id = ApplicationUtils.Object2String(m.get("user_id"));
        	if(ApplicationUtils.isNotNull(user_id)){
        		List<Teacher> lt = teacherService.queryTearcherById(user_id);
        		if(lt!=null&&lt.size()>0){
        			Teacher tModel = lt.get(0);
        			m.put("image_src", tModel.getImage_src());
        			m.put("desc", tModel.getDesc());
        			m.put("score", tModel.getT_score());
        			m.put("user_name", tModel.getUser_name());
        			m.put("sex", tModel.getSex());
        			m.put("birthday", tModel.getBirthday());
        		}
        	}
        	nlist.add(m);
        }
        retD.put("total", pageObj.getTotal());
        retD.put("data", nlist);
        return retD;
    }
	
	/**
	 * @param user_id
	 * @param user_name
	 * @return
	 */
	@RequestMapping(value = "/queryTearcherDetail")
	public ModelAndView queryTearcherDetail(String user_id,String user_name,
			String sex,String birthday,String dept_id,String dept_name){
		Map<String,Object> m = Maps.newHashMap();
		m.put("user_id", user_id);
		m.put("user_name", user_name);
		m.put("birthday", birthday);
		m.put("sex", sex);
		m.put("dept_id", dept_id);
		m.put("dept_name", dept_name);
		List<Teacher> lt = teacherService.queryTearcherById(user_id);
		if(lt!=null&&lt.size()>0){
			Teacher tModel = lt.get(0);
			if(StringUtils.isNotBlank(tModel.getUser_name())){
				m.put("user_name", tModel.getUser_name());
			}
			if(StringUtils.isNotBlank(tModel.getBirthday())){
				m.put("birthday", tModel.getBirthday());
			}
			if(StringUtils.isNotBlank(tModel.getSex())){
				m.put("sex", tModel.getSex());
			}
			m.put("image_src", tModel.getImage_src());
			m.put("desc", tModel.getDesc());
			m.put("t_score", tModel.getT_score());
			m.put("title", tModel.getTitle());
			m.put("teach_glimpse", tModel.getTeach_glimpse());
		}
		return new ModelAndView("main/teacher/info",m);
	}
	
	/**
	 * 保存用户信息
	 * @param user_id
	 * @param desc
	 * @param score
	 * @param image_src
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/saveTeacherInfo")
    @ResponseBody
    public Object saveTeacherInfo(Teacher teacher,HttpServletRequest request){
		boolean hasLogin =ShopOaSessionUtil.setOaSession(request);
		Map<String, Object> parm = new HashMap<String, Object>();		
		Map<String, Object> retD = new HashMap<String, Object>();
		
		if(!hasLogin){
        	retD.put(ApplicationUtils.MSG, "请先登录");
        	retD.put(ApplicationUtils.STATUS, "0");
        	return retD;
		}

        if(StringUtils.isEmpty(teacher.getImage_src())){
        	teacher.setImage_src("http://spera.img-cn-hangzhou.aliyuncs.com/default_image/h_default.png");
        }
        if(StringUtils.isEmpty(teacher.getUser_id())){
        	retD.put(ApplicationUtils.MSG, "用户数据有误");
        	retD.put(ApplicationUtils.STATUS, "0");
        	return retD;
        }
        if(StringUtils.isEmpty(teacher.getT_score())){
        	teacher.setT_score("0");
        }
        parm.put("user_name", teacher.getUser_name());
        parm.put("birthday", teacher.getBirthday());
        parm.put("user_id", teacher.getUser_id());
        parm.put("sex", teacher.getSex());
        parm.put("desc", teacher.getDesc());
        parm.put("t_score", teacher.getT_score());
        parm.put("image_src", teacher.getImage_src());
        parm.put("title", teacher.getTitle());
        parm.put("teach_glimpse", teacher.getTeach_glimpse());
        parm.put("dept_id", teacher.getDept_id());
        parm.put("dept_name", teacher.getDept_name());
        retD = teacherService.saveTeacherInfo(parm,request);
        return retD;
    }
	
	
	/**
	 * 图片单次上传
	 * @param req
	 * @param resp
	 * @param token
	 * @param imagefile
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping(value = "/fileUpload")
	@ResponseBody
	public Object fileUpload(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(value = "file", required = true) MultipartFile file,String fileFileName)
	
			throws ServletException, IOException {
		Map<String,Object> m = Maps.newHashMap();
		if(file==null){
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "请选择上传的文件");
			return m;
		}
		
//		System.out.println(file.getName()+","+);
		String fileType = tools.getFileType(file.getOriginalFilename());
		if(!ApplicationUtils.isNotNull(fileType)){
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "未知附件类型");
			return m;
		}
		
		
		if(!tools.checkFileType(fileType,"jpg","jpeg","bmp","png")){
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "附件类型不允许");
			return m;
		}
		
		String file_token = TokenUtil.generateToken(file.getOriginalFilename(), file.getSize()+"");
		
		try {
			m = new HashMap<String, Object>();
			if (!file.isEmpty()) {
				String newfilename = "oauser_"+file_token+"_"+file.getOriginalFilename();
				Integer fileSize = 0;
				fileSize = (int) file.getSize() / 1048576;
				
				if (fileSize <= 5) {
					String filePath = SperaOSSUtil.springFileUpOss(AliyunContent.fileBucket,"oauser_file/"+UDate.getDate()+"/"+newfilename, file);
					m.put("image_src", filePath);
					m.put(ApplicationUtils.STATUS, "1");
					m.put(ApplicationUtils.MSG, "上传成功");
				} else {
					m.put(ApplicationUtils.STATUS, "0");
					m.put(ApplicationUtils.MSG, "上传的文件太大，文件大小不能超过5M");
					return m;
				}
			} else {
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "请选择上传的文件");
			}

		} catch (Exception e) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "系统异常");
			e.printStackTrace();
		}

		return m;
	}

}
